package windowFactoryPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SearchWindow extends WindowGenerator {
	Query q = new Query();
	// Declare window components
	static JTextField jtfKeyword, jtfUserName;
	static JPasswordField jpfPassword;
	static JRadioButton jrbPrograms, jrbUsers, jrbDevelopers;
	static JComboBox jcbCategory, jcbKeyword;
	private JButton jbtSearch, jbtClear;
	static JButton jbtCreateUser, jbtLogin;
	private JPanel centerPanel, searchPanel, eastPanel, loginPanel,
			radioButtonPanel, keywordPanel, buttonPanel, panel;
	private ResultsPanel resultWindow;
	private JScrollPane resultsPane;

	public SearchWindow() {

		setLayout(new BorderLayout());

		// Instantiate eastPanel
		eastPanel = new JPanel();
		eastPanel.setLayout(new BorderLayout());
		eastPanel.setBorder(BorderFactory.createEtchedBorder());

		// Instantiate and add elements onto eastPanel.CENTER -- User Login info
		loginPanel = new JPanel();
		loginPanel.setLayout(new GridLayout(7, 0, 20, 10));
		loginPanel.setBorder(BorderFactory.createEtchedBorder());
		jtfUserName = new JTextField("Enter User Name", 10);
		jtfUserName.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				jtfUserName.setText("");
				jtfUserName.selectAll();
			}

			@Override
			public void focusLost(FocusEvent e) {
				jtfUserName.select(0, 0);
			}
		});
		jpfPassword = new JPasswordField("Password", 10);
		jpfPassword.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent arg0) {
				jpfPassword.setText("");
				jpfPassword.selectAll();
			}

			@Override
			public void focusLost(FocusEvent arg0) {
				jpfPassword.select(0, 0);
			}
		});
		jpfPassword.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (jpfPassword.hasFocus()) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER)
						setLoginPanel();
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
		jbtLogin = new JButton("Login");
		jbtLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setLoginPanel();
			}
		});
		jbtCreateUser = new JButton("Create User");
		jbtCreateUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// create user pop-up
				AccountPopUp apu = new AccountPopUp();
				apu.setVisible(true);
			}
		});
		loginPanel.add(jbtLogin);
		loginPanel.add(jbtCreateUser);
		loginPanel.add(jtfUserName);
		loginPanel.add(jpfPassword);
		eastPanel.add(loginPanel, BorderLayout.CENTER);

		// Instantiate and add elements onto eastPanel.SOUTH -- RadioButton
		// panel
		radioButtonPanel = new JPanel();
		radioButtonPanel.setLayout(new GridLayout(3, 0, 20, 10));
		radioButtonPanel.setBorder(BorderFactory.createEtchedBorder());
		jrbPrograms = new JRadioButton("Program Name");
		jrbPrograms.setMnemonic(KeyEvent.VK_P);
		jrbUsers = new JRadioButton("User Name");
		jrbUsers.setMnemonic(KeyEvent.VK_U);
		jrbDevelopers = new JRadioButton("Developer Name");
		jrbDevelopers.setMnemonic(KeyEvent.VK_D);
		ButtonGroup bg = new ButtonGroup();
		bg.add(jrbPrograms);
		bg.add(jrbUsers);
		bg.add(jrbDevelopers);
		radioButtonPanel.add(jrbPrograms);
		radioButtonPanel.add(jrbUsers);
		radioButtonPanel.add(jrbDevelopers);
		jrbPrograms.setSelected(true);
		eastPanel.add(radioButtonPanel, BorderLayout.SOUTH);

		add(eastPanel, BorderLayout.EAST);

		// Instantiate centerPanel
		centerPanel = new JPanel(new BorderLayout());
		centerPanel.setBorder(BorderFactory.createEtchedBorder());

		// Instantiate and add elements onto CenterPanel -- Search Options
		searchPanel = new JPanel();
		searchPanel.setLayout(new GridLayout(2, 0));

		// JTextField for keyword searches
		keywordPanel = new JPanel();
		keywordPanel.setLayout(new GridLayout(1, 1));
		jtfKeyword = new JTextField(50);
		keywordPanel.add(jtfKeyword);
		searchPanel.add(keywordPanel);

		// Buttons and JComboBox
		buttonPanel = new JPanel();
		jcbCategory = new JComboBox(
				tester.categoryNames.toArray(new String[tester.categoryNames.size()]));
		jcbCategory.setSelectedIndex(0);
		jcbKeyword = new JComboBox(
				tester.keywordNames.toArray(new String[tester.keywordNames.size()]));
		buttonPanel.add(jcbKeyword);
		buttonPanel.add(jcbCategory);
		jbtSearch = new JButton("Search");
		jbtSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Query.ip_id = 0;
				Query.program_id = 0;
				if (jrbPrograms.isSelected()) {
					Query.progId.clear();
					Query.ipId.clear();
					Query.sourceCodeId.clear();
				}

				if (jrbPrograms.isSelected()) {
					remove(panel);
					dispose();
					q.searchProgram();
					WindowGenerator.generateSearchWindow();
					panel.setVisible(true);
				}
				if (jrbUsers.isSelected()) {
					q.searchUser();
				}
				if (jrbDevelopers.isSelected()) {
					q.searchIP();
				}
			}
		});
		jtfKeyword.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (jtfKeyword.hasFocus()) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						if (jrbPrograms.isSelected()) {
							remove(panel);
							dispose();
							q.searchProgram();
							WindowGenerator.generateSearchWindow();
							panel.setVisible(true);
						}
						if (jrbUsers.isSelected()) {
							q.searchUser();
						}
						if (jrbDevelopers.isSelected()) {
							q.searchIP();
						}
					}
				}
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
		jtfKeyword.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent arg0) {
				toFront();
			}

			@Override
			public void focusLost(FocusEvent arg0) {
			}
		});
		jbtSearch.setMnemonic(KeyEvent.VK_ENTER);
		buttonPanel.add(jbtSearch);
		jbtClear = new JButton("Clear");
		jbtClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jtfKeyword.setText("");
				jrbPrograms.setSelected(true);
				panel.setVisible(false);
			}
		});
		buttonPanel.add(jbtClear);
		searchPanel.add(buttonPanel);

		centerPanel.add(searchPanel, BorderLayout.NORTH);

		// Generate ResultsPanel and add to resultsPane
		resultWindow = new ResultsPanel(Query.progId.size());
		panel = resultWindow.getPanel();
		resultsPane = new JScrollPane(panel);
		resultsPane.getVerticalScrollBar().setUnitIncrement(16);
		resultsPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		centerPanel.add(resultsPane, BorderLayout.CENTER);

		add(centerPanel, BorderLayout.CENTER);
	}

	public void setLoginPanel() {
		if (!tester.UserIsLoggedIn) {
			q.userLogin();
			if (tester.UserIsLoggedIn) {
				jtfUserName.setVisible(false);
				jpfPassword.setVisible(false);
				jbtLogin.setText("Log Out");
				jbtCreateUser.setVisible(false);
				setTitle("Welcome " + tester.userNameLogin + "!");
			} else {
				jtfUserName.setVisible(true);
				jtfUserName.setText("Enter User Name");
				jpfPassword.setVisible(true);
				jpfPassword.setText("Password");
				jbtCreateUser.setVisible(true);
				jbtLogin.setText("Login");
				setTitle("Welcome Guest");
				tester.UserIsLoggedIn = false;
			}
		} else {
			jtfUserName.setVisible(true);
			jtfUserName.setText("Enter User Name");
			jpfPassword.setVisible(true);
			jpfPassword.setText("Password");
			jbtCreateUser.setVisible(true);
			jbtLogin.setText("Login");
			setTitle("Welcome Guest");
			tester.UserIsLoggedIn = false;
			tester.userNameLogin = "";
		}

		System.out.println("user name logged in: " + tester.userNameLogin);
	}

	public static void setSearch(String keyword, String key) {
		if (key.equals("d")) {
			jtfKeyword.requestFocus();
			jtfKeyword.setText(keyword);
			jrbDevelopers.setSelected(true);
		}
		if (key.equals("p")) {
			jtfKeyword.requestFocus();
			jtfKeyword.setText(keyword);
			jrbPrograms.setSelected(true);
		}
		if (key.equals("u")) {
			jtfKeyword.requestFocus();
			jtfKeyword.setText(keyword);
			jrbUsers.setSelected(true);
		}
	}
}
